package com.example.minefieldkeepercloud.activity.restart;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.RestartStatisticsAdapter;
import com.example.minefieldkeepercloud.bean.RestartStatisticsBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 重启统计
 */
public class RestartStatisticsActivity extends AppCompatActivity {


    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;

    @BindView(R.id.tv_starttime)
    TextView tv_starttime;//开始时间
    @BindView(R.id.tv_endtime)
    TextView tv_endtime;//结束时间
    @BindView(R.id.tv_Counts)
    TextView tv_Counts;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<RestartStatisticsBean.DataBean> dataBeanList;
    private RestartStatisticsAdapter restartStatisticsAdapter;//重启统计列表

    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
    private int startOrEnd = 0;//1 开始时间 2 结束时间
    private TimePickerView pvTimeDay;//年月日时间选择器

    private int num=1;
    private boolean IsMore=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restart_statistics);
        ButterKnife.bind(this);
        init();
        initPvTimeYear();

    }

    @OnClick({R.id.ll_starttime, R.id.ll_endtime, R.id.title_left, R.id.tv_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_starttime:
                //开始时间
                startOrEnd = 1;
                //设置选择时间
                pvTimeDay.setTime(getFormatDate(tv_starttime.getText().toString()));
                pvTimeDay.show();
                break;
            case R.id.ll_endtime:
                //结束时间
                startOrEnd = 2;
                //设置选择时间
                pvTimeDay.setTime(getFormatDate(tv_endtime.getText().toString()));
                pvTimeDay.show();
                break;

            case R.id.title_left:
                //取消
                finish();
                break;
            case R.id.tv_search:
                //刷新
                refreshClear();
                break;


        }
    }


    private void init() {


        dataBeanList = new ArrayList<>();
        restartStatisticsAdapter = new RestartStatisticsAdapter(this, dataBeanList);

        lRecyclerViewAdapter = new LRecyclerViewAdapter(restartStatisticsAdapter);
        //空白页
        mRecyclerView.setEmptyView(findViewById(R.id.empty));
        findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshClear();

            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.d_10)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.bg)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {


                    refreshClear();

            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadMoreEnabled(true);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
                Log.i("Json","第"+num+"页");
            if (IsMore){
              getdata();
            }else {
                mRecyclerView.refreshComplete(10);

            }

            }
        });

    }

    /**
     * 刷新数据
     */
    private void refreshClear() {
        num=1;
        dataBeanList.clear();
        restartStatisticsAdapter.notifyDataSetChanged();
        getdata();
        IsMore=true;
    }

    /**
     * 年月日时间选择器
     */
    private void initPvTimeYear() {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //向前走一天
        Date date = calendar.getTime();
        tv_starttime.setText(ymd.format(date));
        tv_endtime.setText(ymd.format(new Date(System.currentTimeMillis())));

        //获取网络数据
        getdata();
        //时间选择器
        pvTimeDay = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置范围
        pvTimeDay.setRange(2017, 2030);

        //设置是否循环滚动
        pvTimeDay.setCyclic(false);
        pvTimeDay.setCancelable(true);

        //时间选择后回调
        pvTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                if (startOrEnd == 1) {
                    tv_starttime.setText(ymd.format(date));
                } else if (startOrEnd == 2) {
                    tv_endtime.setText(ymd.format(date));
                }

            }


        });
    }

    /**
     * 获取网络数据
     */
    private void getdata() {
        if (NetWorkUtil.check(this)) {


            NetHelperNew.GetRe_statistics(num+"",tv_starttime.getText().toString(), tv_endtime.getText().toString(), new ApiHttpCallback(this, 1, true) {
                @Override
                public void onSuccess(String data) {

                    Log.i("-->", "重启统计：" + data);
                    RestartStatisticsBean restartStatisticsBean = MyGson.getInstance().fromJson(data, RestartStatisticsBean.class);


                    if (restartStatisticsBean.getType() == 1) {


                        tv_Counts.setText("重启台次:"+restartStatisticsBean.getCounts()+"台");
                        mRecyclerView.refreshComplete(10);

                        //更新数据
                        dataBeanList.addAll(restartStatisticsBean.getData());
                        Log.d("dataBeanList",dataBeanList.size()+"");

                        restartStatisticsAdapter.notifyDataSetChanged();
                        num++;

                        if (restartStatisticsBean.getData().size()<10){
                            IsMore=false;
                        }

                    } else {
                        mRecyclerView.refreshComplete(10);
                        ToastUtil.showToast(RestartStatisticsActivity.this, restartStatisticsBean.getContent());
//                        new ErrorDialog(getActivity(), staffBean.getContent()).show();
                    }


                }

            });
        }

    }

    /**
     * 转化时间
     *
     * @param s
     * @return
     */
    private Date getFormatDate(String s) {
        try {
            return ymd.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
